<?php

namespace app\api\controller;

use app\common\controller\api\Base;
use app\common\controller\Tools;

class Upload extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }

    private static $fileType = [
        'image_type' => 1,
        'video_type' => 2,
        'file_type' => 3, //TODO 其他文件类型暂不处理
    ];

    public function uploadFile()
    {
        //设置过滤方法
        if ($this->request->isPost()) {
            $tools = new Tools();
            //无类型则默认为图片 1图片文件 2视频
            $type = $this->request->post('type', 1);
            $file = request()->file('file');
            switch ($type) {
                case self::$fileType['image_type']:
                    $ret = $tools->uploadImageFile($type, $file);
                    break;
                case self::$fileType['video_type']:
                    $ret = $tools->uploadVideoFile($type, $file);
                    break;
                default :
                    $ret = [
                        'success' => false,
//                        'msg' => '非法type'
                        'msg' => 'Please upload image file!'
                    ];
            }
            if ($ret['success']) {
                $this->success('上传成功', '', $ret['data']);
            }
            $this->error($ret['msg']);
        }
    }
}